using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Reflection;
using System.IO;

namespace RunAliasCreator
{
	public static class ApplicationData
	{
		private static Assembly _applicationAssembly = Assembly.GetExecutingAssembly();

		/// <summary>
		/// Gets/sets the assembly that contains the current logical application.
		/// </summary>
		public static Assembly ApplicationAssembly
		{
			get { return _applicationAssembly; }
			set { _applicationAssembly = value; }
		}

		/// <summary>
		/// Loads the assembly that called the method as the current logical application.
		/// </summary>
		public static void LoadApplicationAssembly()
		{
			_applicationAssembly = Assembly.GetCallingAssembly();
		}

		/// <summary>
		/// Gets the product name associated with this application.
		/// </summary>
		public static string ProductName
		{
			get
			{
				object[] result = ApplicationAssembly.GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
				return ((AssemblyTitleAttribute)result[0]).Title;
			}
		}

		/// <summary>
		/// Gets the product version associated with this application.
		/// </summary>
		public static Version ProductVersion
		{
			get
			{
				return ApplicationAssembly.GetName().Version;
			}
		}
		
		
		
		/// <summary>
		/// Gets the name, version, and build date of the application in the following format:
		/// &quot;MyApplication - Version 1.0 (Built on 1/1/2000)&quot;
		/// </summary>
		public static string FullProductName
		{
			get
			{
				return ProductName + " - " + VersionAndBuildDate;
			}
		}

		/// <summary>
		/// Gets the version and build date of the application in the following format:
		/// &quot;Version 1.0 (Built on 1/1/2000)&quot;
		/// </summary>
		public static string VersionAndBuildDate
		{
			get
			{
				DateTime buildDate = BuildDate;
				return "Version " + MajorMinorVersion +
					" (Built on " + buildDate.ToShortDateString() +
					" at " + buildDate.ToLongTimeString() + ")";
			}
		}

		/// <summary>
		/// Gets the version of the application without the build and revision numbers.
		/// </summary>
		public static string MajorMinorVersion
		{
			get
			{
				string mmv = ProductVersion.Major + "." + ProductVersion.Minor;
				return mmv;
			}
		}

		/// <summary>
		/// Gets the date and time that the current version of the application was built.
		/// </summary>
		public static DateTime BuildDate
		{
			get
			{
				Version assemblyVersion = ProductVersion;
				DateTime buildDate = new DateTime(2000, 1, 1);
				buildDate = buildDate.AddDays(assemblyVersion.Build);
				buildDate = buildDate.AddSeconds(assemblyVersion.Revision * 2);
				bool isDST = TimeZone.IsDaylightSavingTime(buildDate,
					TimeZone.CurrentTimeZone.GetDaylightChanges(buildDate.Year));
				if (isDST) buildDate = buildDate.AddHours(1);
				return buildDate;
			}
		}

		/// <summary>
		/// Gets the path for the executable file that started the application, not including the executable name.
		/// </summary>
		/// <returns>The startup path of the application.</returns>
		public static string GetStartupPath() { return GetStartupPath(false); }
		/// <summary>
		/// Gets the path for the executable file that started the application, not including the executable name.
		/// </summary>
		/// <param name="alwaysEndsWithBackslash">If true, forces the path returned to end with a backslash (if it does not already); otherwise, the path is returned normally.</param>
		/// <returns>A System.String containing the startup path.</returns>
		public static string GetStartupPath(bool alwaysEndsWithBackslash)
		{
			string dirName = Path.GetDirectoryName(ApplicationAssembly.GetModules()[0].FullyQualifiedName);
			if (alwaysEndsWithBackslash && !dirName.EndsWith("\\")) dirName += "\\";
			return dirName;
		}

		/// <summary>
		/// Gets the path for the executable file that started the application, not including the executable name.
		/// </summary>
		public static string StartupPath { get { return GetStartupPath(); } }

		/// <summary>
		/// Gets the path for the executable file that started the application, including the executable name.
		/// </summary>
		public static string ExecutablePath
		{
			get
			{
				string dirName = Path.GetFileName(ApplicationAssembly.GetModules()[0].FullyQualifiedName);
				return dirName;
			}
		}


		/// <summary>
		/// Gets the specified resource image from the assembly.
		/// </summary>
		/// <param name="resourceId">The ID of the resource to load.</param>
		/// <returns>An Image containing the specified resource.</returns>
		public static Image GetResourceImage(string resourceId)
		{
			try
			{
				string assemblyName = ApplicationAssembly.GetName().Name.Replace(" ", "_");
				Stream file = ApplicationAssembly.GetManifestResourceStream(assemblyName + "." + resourceId);
				return new Bitmap(file);
			}
			catch { return null; }
		}
	}
}
